package com.example.movie.ui.user

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.movie.R
import com.example.movie.common.constants.Constants
import com.example.movie.data.user.factory.UserInfoPageViewModelFactory
import com.example.movie.data.user.model.UserInfoPageViewModel
import com.example.movie.ui.user.adapter.MyMovieListAdapter
import kotlinx.android.synthetic.main.fragment_my_movie_list.*

/**
 * A placeholder fragment containing a simple view.
 */
class MovieListPlaceholderFragment : Fragment() {

    private val userInfoPageViewModel: UserInfoPageViewModel by viewModels(factoryProducer = { UserInfoPageViewModelFactory() })

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        setObserver()
        return inflater.inflate(R.layout.fragment_my_movie_list, container, false)
    }

    private fun setObserver() {
        arguments?.getInt(ARG_SECTION_NUMBER).let { it ->
            it?.apply {
                when (this) {
                    Constants.UserInfo.HOPE_LOOK_LIST -> {
                        userInfoPageViewModel.loadMyMovieList(
                            Constants.UserInfo.HOPE_LOOK_LIST
                        )
                        userInfoPageViewModel.hopeLookList.observe(viewLifecycleOwner, {
                            it?.success?.let {
                                movie_list.apply {
                                    this.layoutManager = LinearLayoutManager(
                                        context,
                                        LinearLayoutManager.VERTICAL,
                                        false
                                    )
                                    val searchResultAdapter =
                                        MyMovieListAdapter(it, Constants.UserInfo.HOPE_LOOK_LIST,requireActivity())
                                    this.adapter = searchResultAdapter
                                    searchResultAdapter.notifyDataSetChanged()
                                    Log.e("debug", "setObserver: 获取想看列表", )
                                }

                            }
                        })

                    }
                    Constants.UserInfo.HAVE_SEEN_LIST -> {
                        userInfoPageViewModel.loadMyMovieList(
                            Constants.UserInfo.HAVE_SEEN_LIST
                        )
                        userInfoPageViewModel.haveSeenList.observe(viewLifecycleOwner, {
                            it?.success?.let {
                                movie_list.apply {
                                    this.layoutManager = LinearLayoutManager(
                                        context,
                                        LinearLayoutManager.VERTICAL,
                                        false
                                    )
                                    val searchResultAdapter =
                                        MyMovieListAdapter(
                                            it,
                                            Constants.UserInfo.HAVE_SEEN_LIST,
                                            requireActivity()
                                        )
                                    this.adapter = searchResultAdapter
                                    searchResultAdapter.notifyDataSetChanged()
                                    Log.e("debug", "setObserver: 获取看过列表", )
                                }
                            }
                        })
                    }
                    else -> {
                    }
                }
            }
        }


    }

    companion object {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private const val ARG_SECTION_NUMBER = "section_number"

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        @JvmStatic
        fun newInstance(sectionNumber: Int): MovieListPlaceholderFragment {
            return MovieListPlaceholderFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }
    }
}